package com.demo.common.vo;

import java.util.HashMap;
import java.util.Map;

/**
 * 返回前端对象
 * @param <T>
 */
public class Result<T> {
    private static final long serialVersionUID = 1L;
    /**
     * 0成功 其他-失败
     */
    private int code;
    /**
     * 返回msg（系统用，一般为空）
     */
    private String msg;
    /**
     * 返回数据
     */
    private T data;
    /**
     * 返回拓展信息，一般为null
     */
    private Map<String, Object> exData;
    /**
     * 返回用户信息，一般报错才有该项
     */
    private String userMsg;

    public <T> Result() {
        this.code = 0;
        this.msg = "success";
        this.userMsg = "success";
    }

    public Result(T data) {
        this.code = 0;
        this.msg = "success";
        this.userMsg = "success";
        this.data = data;
    }

    public <T> Result(int code, String userMsg) {
        this.code = code;
        this.userMsg = userMsg;
    }

    public <T> Result(int code, String msg, String userMsg) {
        this.code = code;
        this.msg = msg;
        this.userMsg = userMsg;
    }

    public static <T> Result<T> error() {
        return error("unknown error");
    }

    public static <T> Result<T> error(String userMsg) {
        return error(5000, userMsg);
    }

    public static <T> Result<T> error(int code, String userMsg) {
        return new Result(code, "error", userMsg);
    }

    public static <T> Result<T> success(T data) {
        return new Result(data);
    }

    public static <T> Result<T> success() {
        return new Result(null);
    }

    public static <T> Result<T> success(String userMsg, T data) {
        Result<T> result = new Result(data);
        result.setUserMsg(userMsg);
        return result;
    }

    public Result put(String key, Object value) {
        if (this.exData == null) {
            this.exData = new HashMap();
        }

        this.exData.put(key, value);
        return this;
    }

    public boolean isSuccess() {
        return this.getCode() == 0;
    }

    public int getCode() {
        return this.code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Map<String, Object> getExData() {
        return this.exData;
    }

    public void setExData(Map<String, Object> exData) {
        this.exData = exData;
    }

    public String getUserMsg() {
        return this.userMsg;
    }

    public void setUserMsg(String userMsg) {
        this.userMsg = userMsg;
    }
}
